package com.sky.controller.admin;

import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @Author by 孙有泽
 * @Date 2025/2/28 16:55
 * @Description 文件上传
 */
@RestController
@RequestMapping("/admin/common")
@Slf4j
@Api(tags = "文件上传")
public class UploadController {

    @Autowired
    private AliOssUtil aliOssUtil;
    @PostMapping("/upload")
    @ApiOperation("value = 文件上传")
    public Result upload(MultipartFile file) throws IOException {

  /*   本地上传
        String originalFilename = file.getOriginalFilename();
        //E:\开发\苍穹外卖\\upload
        String exName = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileName=UUID.randomUUID()+ exName;
        file.transferTo(new File("e:\\开发\\苍穹外卖\\upload\\"+fileName));
   */
        String originalFilename = file.getOriginalFilename();
        String exName = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileName=UUID.randomUUID()+ exName;
        try {
            String upload = aliOssUtil.upload(file.getBytes(), fileName);
            return Result.success(upload);
        }catch (IOException e){
            log.error("文件上传失败：{}",e.getMessage());
            return Result.error(MessageConstant.UPLOAD_FAILED);
        }


    }
}
